/*
 * @Date: 2020-08-26 14:37:41
 * @LastEditors: xujianxing
 * @LastEditTime: 2020-08-26 14:48:36
 * @FilePath: \nili\src\common\interceptor\exception.interceptor.ts
 */
import {
  NestInterceptor,
  ExecutionContext,
  CallHandler,
  BadGatewayException,
  Injectable,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

// 异常拦截，可覆盖抛出的异常

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
  intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<any> | Promise<Observable<any>> {
    return next
      .handle()
      .pipe(catchError(err => throwError(new BadGatewayException())));
  }
}
